import json

from django.http import HttpResponse,Http404,StreamingHttpResponse
from django.shortcuts import render
from django.http import JsonResponse
# from user import models
# Create your views here.
def hello(request):
    return HttpResponse('你好!')


def index(request):
    return render(request,'index.html')

def login(request):
    if request.POST:
        # request.POST['q']
        # request.POST.get('q')
        user = request.POST['username']
        # print(request.POST['username'])
        # print(request.POST.get('password'))
        try:
            user = models.User.objects.get(username=user)
        except:
            return HttpResponse('Question does not exist')
        pwd = request.POST.get('password')
        if user.password == pwd:
            request.session['login'] = '已经登录'
            return render(request, 'deng.html', context={'username': user})
        else:
            return render(request,'login.html')
    # get方法
    print(request.GET['id'])
    return render(request, 'login.html')




# 返回json数据
def getjson(request):
    myjson = {
        'username':'蔡',
        'password':'123456'
    }
    # return JsonResponse(myjson,safe=False)
    return HttpResponse(json.dumps(myjson),content_type='application/json')

def json2(request):
    result = json.loads(request.body)
    print(result['ID'])
    return render(request, 'login.html')

# def header(request):
#     return render(request,'header.html')


